<?php
namespace App\HttpController\Admin;

use App\Common\Controller\Rest;
use App\Validate\AdminUserInfo;
class Users extends Rest
{
    public $modelName = 'UserInfo';

    /**
     * 会员信息录入
     */
    public function POSTIndex()
    {
        $id = $this->param['userId'];
        $data = DB::table('admin_userinfo')->where('uid',$id)->find();
        $this->result(true,'msg'=>'查询成功','data'=>$data);
    }
    public function POSTEdit()
    {
        $id = $this->param['userId'];
        $data = $this->param;
        if($param['type'] == '1'){
            //新增
            $validate = new Validate(
                [
                    'sex' => 'require',
                    'height' => 'require',
                    'weight' => 'require',
                    'waist' => 'require',
                    'birthday' => 'require',
                    'blood' => 'require',
                    'sugar' => 'require',
                    'depart' => 'require',
                    'company' => 'require',
                    'address' => 'require',
                ],
                [
                    'sex.require' => '性别必须填写',
                    'height.require' => '身高必须填写',
                    'weight.require' => '体重必须填写',
                    'waist.require' => '胸围必须填写',
                    'birthday.require' => '出生年月必须填写',
                    'blood.require' => '血压必须填写',
                    'sugar.require' => '血糖必须填写',
                    'depart.require' => '职务必须填写',
                    'company.require' => '工作单位必须填写',
                    'address.require' => '地址必须填写',
                ]
            );
            $this->abort($validate->check($this->param),['msg'=>$validate->getError()]);
            $data = unset($param['type']);
            $data = unset($param['userId']);
            $data['uid'] = $id;
            $data['addtime'] = date('Y-m-d H:i:s');
            $res = DB::table('admin_userinfo')->insert($data);
            if($res){
                $this->result($res,'msg'=>'添加成功','data'=>true);
            }
        }elseif($param['type'] == '2'){
            $data = unset($param['type']);
            $data = unset($param['userId']);
            $res = DB::table('admin_userinfo')->where('uid',$id)->update($data);
            if($res){
                $this->result($res,'msg'=>'修改成功','data'=>true);
            } 
        }
        $this->result(false,'msg'=>'操作失败','data'=>false);
    }
}
